var router = require('express').Router();
var util = require('util');
var EventProxy = require('eventproxy');
var async = require('async');

var Consts = require('../consts');
var resultJson = require('../result-json');
var Utils = require('../utils');
var logger = require('../models/logger');
var TrainPoint = require('../models/TrainPoint');
var Direction = require('../models/Direction');

router.post('/', function(req, res) {
	res.send('respond with a resource');
});

router.post('/list', function(req, res) {
	var que = req.body;
	var proxy = new EventProxy();
	proxy.all("results", "count", function(results, count) {
		res.send(resultJson(0, {
			results: results,
			count: count
		}))
	})
	var start = que.start || 0;
	var count = que.count || Consts.MAX;
	TrainPoint.find().sort({
		create: -1
	}).skip(start).limit(count).exec(function(err, companies) {
		if (err) {
			logger.error(err);
			return res.send(resultJson(2));
		}
		var companies = JSON.parse(JSON.stringify(companies));
		var result = [];
		async.each(companies, function(item, cb) {
			Direction.findOne({
				_id: item.direction
			}, {
				courses: 0,
				projects: 0
			}).exec(function(err, direction) {
				if (err) {
					logger.error(err);
					return res.send(resultJson(2));
				}
				item.direction = {};
				item.direction = direction
				result.push(item);
			});
		}, function(err) {
			proxy.emit("results", result);
		});
	});
	TrainPoint.count(function(err, num) {
		if (err) {
			logger.error(err);
			return res.send(resultJson(2));
		}
		proxy.emit("count", num);
	});
});

router.post('/detail', function(req, res) {
	var _id = req.body._id;
	if (!_id)
		return res.send(resultJson(3));

	TrainPoint.findOne({
		_id: _id
	}).exec(function(err, trainpoint) {
		if (err) {
			logger.error(err);
			return res.send(resultJson(2));
		}
		return res.send(resultJson(0, trainpoint));
	});
});

//添加或修改公司信息
router.post('/upsert', function(req, res) {
	var trainpoint = req.body;
	trainpoint = JSON.stringify(trainpoint);
	trainpoint = trainpoint.replace(/\</g, '&lt;').replace(/\>/g, '&gt;').replace(/</g, '&lt;').replace(/>/g, '&lg;');
	trainpoint = JSON.parse(trainpoint);
	if (!trainpoint.name || !trainpoint.addr)
		return res.send(resultJson(3));

	if (trainpoint._id) { //update
		TrainPoint.count({
			name: trainpoint.name,
			_id: {
				$ne: trainpoint._id
			}
		}, function(err, cnt) {
			if (err)
				return res.send(resultJson(2));
			if (cnt)
				return res.send(resultJson(402));
			TrainPoint.update({
				_id: trainpoint._id
			}, {
				$set: trainpoint
			}).exec(function(err) {
				if (err) {
					logger.error(err);
					return res.send(resultJson(2));
				}
				res.send(resultJson(0));
			});
		});
	} else { //add
		//名字唯一性判断
		TrainPoint.count({
			name: trainpoint.name
		}, function(err, cnt) {
			if (err)
				return res.send(resultJson(2));
			if (cnt)
				return res.send(resultJson(402));

			trainpoint.create = new Date();
			new TrainPoint(trainpoint).save(function(err, trainpoint) {
				if (err) {
					logger.error(err);
					return res.send(resultJson(2));
				}
				res.send(resultJson(0, trainpoint));
			});

		});
	};
});

//删除
router.post('/remove', function(req, res) {
	var trainpoint = req.body;
	TrainPoint.remove({
		_id: trainpoint._id
	}, function(err) {
		if (err) {
			logger.error(err);
			return res.send(resultJson(2));
		}
		res.send(resultJson(0));
	})
});

module.exports = router;